<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>问答-聊天页面</title>
<style>
    *{
      margin:0;
      padding:0;
      box-sizing:border-box;
    }

    .chatpdf{
        display: flex;
        height: 100vh;
        flex-direction: row;
    }
    .chatpdf .pannel{
        width: 350px;
        color:#fff;
        background-color: rgb(8, 28, 46);
        font-family: Arial;
        font-size: 24px;
        text-align: center;
        padding:10px;
    }

    .recommend-list{
        background: #fff;
        border-radius: 18px;
        display: flex;
        justify-content: center;
        padding:10px;
        font-size: 16px;
        color:#333;
    }
    .recommend-item{
        width:100px;
    }
    .item-img{
        width:100%;
        height: 60px;
        border: 10px;
    }
    .item-img img{
        height: 100%;
    }

    .chatpdfBox{
        display: flex;
        flex-direction: column;
        flex: 1;
        background: linear-gradient(to bottom right,#dbe6fb, #f3f4f8);
        background-size: cover;
        background-attachment: fixed;
    }
    .chatpdfLine{
        flex: 1;
        width: 100%;
        max-width: 1000px;
        margin: 0 auto;
        overflow-y: auto;
    }
    .chatpdfRow{
        margin: 20px 10px;
        display: flex;
    }
    .chatpdfAsk{
        justify-content: flex-end;
    }
    .chatpdfContent{
        display: inline-block;
        border-radius: 8px;
        padding: 6px 12px;
        max-width: 500px;
        background: rgba(255, 255, 255, 0.6);
        font-size: 14px;
        box-shadow:  0px 0.3px 0.9px rgba(0, 0, 0, 0.12), 0px 1.6px 3.6px rgba(0, 0, 0, 0.16);
    }
    .chatpdfAsk .chatpdfContent{
        background: linear-gradient(90deg, #2870EA 10.79%, #1B4AEF 87.08%);;
        color: #fff;
    }
    .chatpdfArea{
        display: flex;
        padding: 5px 10px;
        max-width: 1000px;
        margin: 0 auto;
        width: 100%;
    }
    .chatpdfArea input{
        flex: 1;
        border-color: #d9d9d9;
        resize: none;
        outline: none;
        padding: 0px 5px;
        height: 35px;
        line-height: 30px;
        color: #404040;
        border-radius: 10px 0px 0px 10px;
        transition: all 0.3s,height 0s;
    }
    .chatpdfArea input:hover{
        border-color: #4096ff;
    }
    .chatpdfArea button{
        height: 35px;
        color: #fff;
        background: linear-gradient(90deg, #2870EA 10.79%, #1B4AEF 87.08%);
        box-shadow: 0 2px 0 rgba(5, 145, 255, 0.1);
        border: none;
        padding: 0 20px;
        border-radius: 0px 8px 8px 0px;
        cursor: pointer;
    }
    .chatpdfArea button:hover{
        background-color: #388aff;
    }
    .chatpdf .fileTitle{
        background-color: #1677ff;
        color: #fff;
        border-radius: 8px;
        padding: 10px;
        margin: 10px;
        font-size: 14px;
        cursor: pointer;
    }
    @media (max-width: 768px) {
        .pannel{
            display: none;
        }
    }
</style>
</head>
<body>
  <div class="chatpdf">
      <div class="pannel">
          <div class="profile">用户名</div>
          <div class="recommend-list">
          </div>
      </div>
      <div class="chatpdfBox">
          <div class="chatpdfLine">
          </div>
          <div class="chatpdfArea">
              <input type="text" class="input-question" name="question"></input>
              <button class="send-btn">
                  <svg t="1682141916531" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1271" width="16" height="16"><path d="M0 1024l106.496-474.112 588.8-36.864-588.8-39.936L0 0l1024 512z" fill="#ffffff" p-id="1272"></path></svg>
              </button>
          </div>
      </div>
  </div>
</body>
<script>
    window.onload = function(){
        const user = JSON.parse(localStorage.getItem("userInfo"))
        if(user){
            renderProfile(user.userName)
        }else{
           location.href = "./login.html"
        }
    }

    let messages = []
    let recommends = []
    const chatPanel = document.querySelector(".chatpdfLine")
    const sendButton = document.querySelector(".send-btn")
    const messagePanel = document.querySelector(".chatpdfLine")
    const recommendPanel = document.querySelector(".recommend-list")
    const questionInput = document.querySelector(".input-question")
    const profilePanel = document.querySelector(".profile")

    const courseItem = `<div class="recommend-item">
                  <div class="item-img">
                      <img src="{{itemImage}}"></img>
                  </div>
                  <div class="item-name">
                        {{itemName}}
                  </div>
              </div>`

    const getRecommendList = function(){
        fetch("/course/listRecommend").then(res => {
            return res.json()
        }).then(data => {
            console.log("---获取推荐列表数据---")
            console.log(data.data)
            recommends = data.data
            renderRecommends()
        })
    }

    const getMessages = function(){
        fetch("/chat/listAll")
            .then(res => {
            return res.json()
            }).then(data => {
            console.log("---获取聊天数据---")
            console.log(data.data)
            messages = data.data
            renderMessages()
         })
    }
    const sendMessage = function(question){
        const sendUrl = "/chat/send?question="+question
        fetch(sendUrl).then(res =>{
            return res.json()
        }).then(data => {
            console.log(data)
            messages.push(data.data)
            renderMessages()
        })
    }

    const handleSendMessage = function(){
        const question = questionInput.value
        if(question === null || question === ""){
            return
        }
        sendMessage(question)
        questionInput.value = ""
    }

    const renderProfile = function(username){
        profilePanel.innerHTML = username
    }



    const renderCourseElement = function(course){
       let rowElement = courseItem.replaceAll(/{{itemImage}}/gi,course.courseImage)
        console.log(rowElement)
        rowElement = rowElement.replaceAll(/{{itemName}}/gi,course.courseName)
        recommendPanel.innerHTML += rowElement
    }

    const renderChatElement = function(type,content){
        const chatDom = document.createElement("div")
        const chatContent = document.createElement("div")

        chatContent.classList.add("chatpdfContent")
        chatDom.classList.add("chatpdfRow")

        chatDom.appendChild(chatContent)
        chatContent.innerText = content
        type === "question" && chatDom.classList.add("chatpdfAsk")
        messagePanel.appendChild(chatDom)
    }

    const renderRecommends = function(){
        recommendPanel.innerHTML = ""
        for(const course of recommends){
            renderCourseElement(course)
        }
    }

    const renderMessages = function(){
        messagePanel.innerHTML = ""
        for(const message of messages){
            renderChatElement("question",message.question)
            renderChatElement("answer",message.answer)
        }
        chatPanel.scrollTo(0,chatPanel.offsetHeight)
    }

    const init = function(){
        sendButton.addEventListener("click",function(e){
            e.preventDefault()
            handleSendMessage()
        })
        getMessages()

        getRecommendList()

    }

    init()


</script>
</html>

